<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn ">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
        content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>三维地图 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>


  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
     include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d,mars3d-wind"></script>

  <!--左上角面板gui-->
  <script src="../lib/datgui/dat.gui.min.js"></script>

  <link href="css/style.css" rel="stylesheet" />
  <style>
    .infoview {
      padding: 0
    }
    .infoview div {
        margin-top: 0;
    }
  </style>
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>


  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          center: {
            y: 20.648765,
            x: 129.340334,
            z: 19999976.27,
            heading: 354.8,
            pitch: -88.9,
            roll: 0,
          },
        },
        control: {
          sceneModePicker: false,
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      map.basemap = 2017 //蓝色底图

      var windLayer = new mars3d.layer.WindLayer({
        maxParticles: 9000,
        particleHeight: 1.0,
        fadeOpacity: 0.996,
        dropRate: 0.003,
        dropRateBump: 0.01,
        speedFactor: 0.2,
        lineWidth: 4.0,
      })
      map.addLayer(windLayer)

      DataProcess.loadData('http://data.marsgis.cn/file/apidemo/wind.nc', 'http://data.marsgis.cn/file/apidemo/windColor.json').then((data) => {
        windLayer.setData(data)
      })

      //参数调整面板
      function onParticleSystemOptionsChange() {
        windLayer.setOptions()
      }

      var gui = new dat.GUI({
        autoPlace: false,
      })
      gui.add(windLayer, 'maxParticles', 1, 256 * 256, 1).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'particleHeight', 1, 10000, 1).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'fadeOpacity', 0.9, 0.999, 0.001).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'dropRate', 0.0, 0.1).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'dropRateBump', 0, 0.2).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'speedFactor', 0.05, 1).onFinishChange(onParticleSystemOptionsChange)
      gui.add(windLayer, 'lineWidth', 0.01, 16.0).onFinishChange(onParticleSystemOptionsChange)

      gui.domElement.classList.add('infoview')
      document.body.appendChild(gui.domElement)
    }

    //数据加载类
    var DataProcess = (function () {
      var data

      var loadNetCDF = function (filePath) {
        return new Promise(function (resolve) {
          var request = new XMLHttpRequest()
          request.open('GET', filePath)
          request.responseType = 'arraybuffer'

          request.onload = function () {
            var arrayToMap = function (array) {
              return array.reduce(function (map, object) {
                map[object.name] = object
                return map
              }, {})
            }

            var NetCDF = new netcdfjs(request.response)
            var dimensions = arrayToMap(NetCDF.dimensions)

            var variables = arrayToMap(NetCDF.variables)
            var uAttributes = arrayToMap(variables['U'].attributes) //
            var vAttributes = arrayToMap(variables['V'].attributes)

            var arrLon = NetCDF.getDataVariable('lon').flat()
            var arrLat = NetCDF.getDataVariable('lat').flat()
            var arrLev = [1] //NetCDF.getDataVariable('lev').flat()

            var arrU = NetCDF.getDataVariable('U').flat()
            var maxU = uAttributes['max'].value
            var minU = uAttributes['min'].value

            var arrV = NetCDF.getDataVariable('V').flat()
            var maxV = vAttributes['max'].value
            var minV = vAttributes['min'].value

            data = {}
            data.dimensions = {} //dimensions: {lon: 720, lat: 361, lev: 1}
            data.dimensions.lon = arrLon.length
            data.dimensions.lat = arrLat.length
            data.dimensions.lev = arrLev.length

            data.lon = {} //lon: {min: 0, max: 359.5}
            data.lon.min = Math.min(...arrLon)
            data.lon.max = Math.max(...arrLon)

            data.lat = {} //lat: {min: -90, max: 90}
            data.lat.min = Math.min(...arrLat)
            data.lat.max = Math.max(...arrLat)

            data.lev = {} // lev: {min: 1, max: 1}
            data.lev.min = Math.min(...arrLev)
            data.lev.max = Math.max(...arrLev)

            data.U = {}
            data.U.array = new Float32Array(arrU)
            data.U.min = minU
            data.U.max = maxU

            data.V = {}
            data.V.array = new Float32Array(arrV)
            data.V.min = minV
            data.V.max = maxV

            resolve(data)
          }

          request.send()
        })
      }
      var loadText = function (filePath) {
        var request = new XMLHttpRequest()
        request.open('GET', filePath, false)
        request.send()
        return request.responseText
      }

      var loadColorTable = function (filePath) {
        var string = loadText(filePath)
        var json = JSON.parse(string)

        var colorNum = json['ncolors']
        var colorTable = json['colorTable']

        var colorsArray = new Float32Array(3 * colorNum)
        for (var i = 0; i < colorNum; i++) {
          colorsArray[3 * i] = colorTable[3 * i]
          colorsArray[3 * i + 1] = colorTable[3 * i + 1]
          colorsArray[3 * i + 2] = colorTable[3 * i + 2]
        }

        data.colorTable = {}
        data.colorTable.colorNum = colorNum
        data.colorTable.array = colorsArray
      }

      var loadData = async function (ncFilePath, colorTableFilePath) {
        await loadNetCDF(ncFilePath)
        loadColorTable(colorTableFilePath)
        return data
      }

      return {
        loadData: loadData,
      }
    })()
  </script>
</body>

</html>
